//----------------------------------Include-------------------------------------
#include <stdint.h>
#include "mcu.h"
//------------------------------------------------------------------------------

//-----------------------------------Macro--------------------------------------
#define countof(Obj)    (sizeof(Obj)/sizeof(Obj[0]))
#define _PIN_N          (1)
//------------------------------------------------------------------------------

//------------------------------------Type--------------------------------------
typedef struct{
  uint32_t   sample_period;  //(设置值) 采样周期
  uint32_t   confirm_times;  //(设置值) 这么多次的采样值都一致，才确认有效，这个次数不能超过32
  uint32_t   (*io)(void);    //采样函数
  
  uint32_t   tick;           //(过程变量) clk 计数，计到采样周期，采样一次
  uint32_t   sample_value;   //(过程变量) 采样值在这个变量里移位
  int32_t    result;         //(结果)     1 确认为高电平  0 确认为低电平  -1 不确定
}t_in_status;
//------------------------------------------------------------------------------

//-----------------------------------Const--------------------------------------
static  const  uint32_t table[32] = {
0x00000001,0x00000003,0x00000007,0x0000000F,0x0000001F,0x0000003F,0x0000007F,0x000000FF,
0x000001FF,0x000003FF,0x000007FF,0x00000FFF,0x00001FFF,0x00003FFF,0x00007FFF,0x0000FFFF,
0x0001FFFF,0x0003FFFF,0x0007FFFF,0x000FFFFF,0x001FFFFF,0x003FFFFF,0x007FFFFF,0x00FFFFFF,
0x01FFFFFF,0x03FFFFFF,0x07FFFFFF,0x0FFFFFFF,0x1FFFFFFF,0x3FFFFFFF,0x7FFFFFFF,0xFFFFFFFF,
};
//------------------------------------------------------------------------------

//---------------------------------Static Var-----------------------------------
static  t_in_status         input_pin[_PIN_N];
//------------------------------------------------------------------------------

//--------------------------------Static Func-----------------------------------
static uint32_t _get_input_0     (void){ return gpio_read_pin(HPM_GPIO0,  GPIO_DI_GPIOA,  9); }
//------------------------------------------------------------------------------

//--------------------------------Public Func-----------------------------------
void mcu_init_di(void)
{
//periph clock
  
//pin
  //PA09 -- key  按下为0
  /* configure pad setting: pull enable and pull down, schmitt trigger enable */
  /* enable schmitt trigger to eliminate jitter of pin used as button */
  //PE -- pull enable      1=enable
  //PS -- pull select      0=pull down  1=pull up
  //HYS-- schmitt trigger  1=enable
  uint32_t pad_ctl = IOC_PAD_PAD_CTL_PE_SET(1) | IOC_PAD_PAD_CTL_PS_SET(0) | IOC_PAD_PAD_CTL_HYS_SET(1);
  HPM_IOC->PAD[IOC_PAD_PA09].FUNC_CTL = IOC_PA09_FUNC_CTL_GPIO_A_09;
  HPM_IOC->PAD[IOC_PAD_PA09].PAD_CTL  = pad_ctl;

  gpio_set_pin_input(HPM_GPIO0,  GPIO_DI_GPIOA,  9);
  
//设置输入采样参数
  input_pin[0].sample_period  = 10;  //ms
  input_pin[0].confirm_times  = 20;  //应小于32
  input_pin[0].io             = _get_input_0;
  input_pin[0].tick           = 0;
  input_pin[0].sample_value   = 0;
  input_pin[0].result         = -1;
}

//period call
void imcu_digit_input(void)
{
  uint32_t     i;
  #define  d   input_pin[i]
  #define  io  input_pin[i].io
  for(i=0; i<countof(input_pin); i++){
    if(++d.tick >= d.sample_period){
      d.tick = 0;
      d.sample_value = (d.sample_value << 1) | (io()?1:0);
      if(table[d.confirm_times-1] == (table[d.confirm_times-1] & d.sample_value)){  //all 1
        d.result = 1;
      }else if(0 == (table[d.confirm_times-1] & d.sample_value)){   //all 0
        d.result = 0;
      }else{    //not comfirm
        d.result = -1;
      }
    }
  }
  #undef  io
  #undef  d
}
//------------------------------------EOF---------------------------------------






